const app = getApp()
Page({
  data: {
    kefuphone:'',
    loadingHidden: false,//代表出现加载
    schoolname: '',
    id:'',
    school_data:{}
  },
  onLoad: function (options) {
    var that=this;
    var id = options.school_id;
    that.getschooldata(id);
    var schoolname = options.schoolname;
    that.setData({
      schoolname: schoolname,
      id: id
    })
    wx.setNavigationBarTitle({
      title: "选择学校"
    })
  },
  //获取学校数据
  getschooldata: function (id) {
    var that = this;
    wx.request({
      url: getApp().globalData.baseUrl + 'getallschool',
      data: {
      },
      method: "get",
      header: {},
      success: function (res) {
        if(res.data.code==1){
          var school_data = res.data.data;
          for (const x in school_data) {
            if (school_data[x].id == id) {
              school_data[x].checked = true;
            } else {
              school_data[x].checked = false;
            }
          }
          that.setData({school_data:school_data})
        }
        that.setData({ loadingHidden: true });
      }
    })
  },
  selectschool: function (e) {
    var id = e.currentTarget.dataset.id;
    var schoolname = e.currentTarget.dataset.name;
    var link = e.currentTarget.dataset.link;
    if (id == "" || id == null || id==undefined){
      wx.showToast({
        title: '网络错误,重新选择',
        image: '/images/error.png',
        duration: 2000
      })
      timer = setTimeout(function () {
        wx.redirectTo({
          url: '../binding/binding'
        })
      }, 2000);
      return;
    }
    let pages = getCurrentPages();
    let currPage = null; //当前页面
    let prevPage = null; //上一个页面
    if (pages.length >= 2) {
      currPage = pages[pages.length - 1]; //当前页面
      prevPage = pages[pages.length - 2]; //上一个页面
      if (prevPage) {
        prevPage.setData({
          schoolname: schoolname,
          id: id,
          link:link,
          user_id:'',
          user_name:'',
          class_text:'',
          user_type:''
        });
      }
    }
    wx.navigateBack({
      delta: 1
    })
  },

  selectradio: function (e) {
    var school_name = this.data.school_name;
    var school_id = this.data.school_id;
    console.log(school_name);
    let pages = getCurrentPages();
    let currPage = null; //当前页面
    let prevPage = null; //上一个页面
    if (pages.length >= 2) {
      currPage = pages[pages.length - 1]; //当前页面
      prevPage = pages[pages.length - 2]; //上一个页面
      if (prevPage) {
        prevPage.setData({
          school_name: school_name,
          school_id: school_id
        });
      }
    }
    wx.navigateBack({
      delta: 1
    })
  },
  onReady: function () {
  },
  onShow: function () {
    var that=this;
    var pages = getCurrentPages();
    var currPage = pages[pages.length - 1];
    if (currPage.data.school_id) {
      that.setData({ school_id: currPage.data.school_id });
    }
    if (currPage.data.school_name) {
      that.setData({ school_name: currPage.data.school_name });
    }
  },
  onHide: function () {
  },
  onUnload: function () {
  },
  onPullDownRefresh: function () {
  },
  onReachBottom: function () {
  },
  onShareAppMessage: function () {
  }
})